﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdexApiClient.api.v2
{
    public partial class EdexApiContext
    {

        /// <summary>
        /// Получить товар
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>       
        public async Task<CommonApiResponse<WH_ITEMS>> GetItemDetails(Guid id)
        {
            var uri = String.Format("v2/items/{0}/?api_key={1}",id.ToString(), this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            var data = await this.GetData<WH_ITEMS>(uri);
            return data;
        }

        public async Task<CommonApiResponse> AddItem(WH_ITEMS data)
        {
            var uri = String.Format("WH_ITEMS/?api_key={0}", this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.Post<WH_ITEMS>(uri, data);
        }

        public async Task<CommonApiResponse> UpdateItem(WH_ITEMS data)
        {
            var uri = String.Format("WH_ITEMS/?api_key={0}", this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.Put<WH_ITEMS>(uri, data);
        }

        public async Task<CommonApiResponse> DeleteItem(string id)
        {
            var uri = String.Format("WH_ITEMS/{0}/?api_key={1}", id, this.api_key);
            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.Delete(uri);
        }

        public async Task<CommonApiResponse<WH_ITEMS>> GetItems(
            string search_ITEM_NAME = null,
            string search_EXT_KEY = null,
            string search_ARTICLE = null,
            bool search_IS_ALL = false,
            string[] expand = null, 
            string[] orderby = null)
        {

            var uri = String.Format("v2/items/?api_key={0}", this.api_key);

            //if (!String.IsNullOrEmpty(search))
            //    uri += "&$filter=substringof('" + search + "', ITEM_NAME) eq true or substringof('" + search + "', EXT_KEY) eq true or substringof('" + search + "', ARTICLE) eq true &$top=100";

            var _query = String.Empty;
            var _queryFilter = String.Empty;
            var _queryExpand = "COMPANY";
            var _queryOrderBy = "ITEM_NAME";
            var _queryTop = "100";                       

            if (!String.IsNullOrEmpty(search_ITEM_NAME))
            {
                _queryFilter += String.Format("substringof('{0}', ITEM_NAME) eq true", search_ITEM_NAME);
            }

            if (!String.IsNullOrEmpty(search_EXT_KEY))
            {
                if(!String.IsNullOrEmpty(_queryFilter))
                    _queryFilter += String.Format(" or substringof('{0}', EXT_KEY) eq true", search_EXT_KEY);
                else
                    _queryFilter += String.Format("substringof('{0}', EXT_KEY) eq true", search_EXT_KEY);
            }         

            if (!String.IsNullOrEmpty(search_ARTICLE))
            {
                if (!String.IsNullOrEmpty(_queryFilter))
                    _queryFilter += String.Format(" or substringof('{0}', ARTICLE) eq true", search_ARTICLE);
                else
                    _queryFilter += String.Format("substringof('{0}', ARTICLE) eq true", search_ARTICLE);
            }

            if (!String.IsNullOrEmpty(_queryFilter))
                _query += "&$filter=" + _queryFilter;
            

            _query += "&$expand=" + _queryExpand;
            _query += "&$orderby=" + _queryOrderBy;

            if(search_IS_ALL == false)
                _query += "&$top=" + _queryTop;

            uri += _query;

            uri = System.IO.Path.Combine(this.ApiUri, uri);
            return await this.GetData<WH_ITEMS>(uri);
        }
    }
}
